package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author ：daqiang
 * @date ：Created in 2019/11/22
 * @description ：
 * @version: 1.0
 */

public class PoiDemo {

    //读取Excel文件
    public static void main1(String[] args) throws IOException {
        //创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\86175\\Desktop\\test.xlsx");
        //创建工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取行对象
//        XSSFRow row = sheet.getRow(0);
//        System.out.println(row);
        //获取单元格对象
//        XSSFCell cell = row.getCell(0);
//        System.out.println(cell);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            //改行有多少个单元格
            short lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row.getCell(j);

                //获取单元格的类型
                int cellType = cell.getCellType();
                if (cellType== Cell.CELL_TYPE_STRING){
                    System.out.println(cell);
                }else if (cellType== Cell.CELL_TYPE_NUMERIC){

                    System.out.println(cell);
                }
            }
        }
    }

    public static void main2(String[] args) throws IOException {
        //创建工作簿对象
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook("C:\\Users\\86175\\Desktop\\test.xlsx");
        //创建工作表对象
        XSSFSheet sheet = xssfWorkbook.getSheet("sheet1");

        for (Row row : sheet) {
            for (Cell cell : row) {
                System.out.println(cell);
            }
        }

    }

    public static void main(String[] args) throws IOException {
        //获取工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet sheet = workbook.createSheet("test");
        //创建行对象
        XSSFRow row = sheet.createRow(0);
        //创建单元格对象
        XSSFCell cell1 = row.createCell(0);
        //给单元格赋值
        cell1.setCellValue("北京");
        //创建单元格对象
        XSSFCell cell2 = row.createCell(1);
        //给单元格赋值
        cell2.setCellValue("上海");

        //创建行对象
        XSSFRow row1 = sheet.createRow(1);
        //创建单元格对象
        XSSFCell cell3 = row1.createCell(0);
        //给单元格赋值
        cell3.setCellValue("五环");
        //创建单元格对象
        XSSFCell cell4= row1.createCell(1);
        //给单元格赋值
        cell4.setCellValue("二百环");

        //创建输出流对象
        OutputStream outputStream = new FileOutputStream("C:\\Users\\86175\\Desktop\\test.xlsx");

        //写入文件中
        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();

    }
}
